package com.szgr.baoyin.activity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.RatingBar;

import com.android.volley.AuthFailureError;
import com.android.volley.Request;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.szgr.baoyin.MyApplication;
import com.szgr.baoyin.R;
import com.szgr.baoyin.adapter.GoodsJudgeListAdapter;
import com.szgr.baoyin.model.OrderInfo;
import com.szgr.baoyin.myview.NoScrollListView;
import com.szgr.baoyin.utils.Constant;
import com.szgr.baoyin.utils.RLog;
import com.szgr.baoyin.utils.ToastUtils;
import com.szgr.baoyin.utils.ViewFindUtils;

import org.json.JSONException;
import org.json.JSONObject;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class GoodsJudgeActivity extends BaseActivity {

    private List<OrderInfo.MyOrderItem.ExtendOrderGoods> data;
    private NoScrollListView goodsListView;
    private RatingBar rb_01;
    private RatingBar rb_02;
    private RatingBar rb_03;
    private CheckBox cb_noName;
    private String order_id;
    private Handler mHandler = new Handler(){
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what){
                case Constant.REQUEST_SUCCESS:
                    ToastUtils.showToast("评价成功");
                    Intent intent = new Intent(GoodsJudgeActivity.this,MyOrderActivity.class);
                    intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK);
                    startActivity(intent);
                    break;
                case Constant.REQUEST_FAIL:
                    String error = (String) msg.obj;
                    ToastUtils.showToast(error);
                    break;
            }
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        setContentView(R.layout.activity_goods_judge);
        super.onCreate(savedInstanceState);
    }

    @Override
    public void initActionBar() {
        actionBarTitle.setText("商品评价");
    }

    @Override
    public void initView() {
        Intent intent = getIntent();
        if(intent != null && intent.hasExtra("extendsgoods")){
            OrderInfo orderInfo = (OrderInfo) intent.getSerializableExtra("extendsgoods");
            List<OrderInfo.MyOrderItem> order_list = orderInfo.getOrder_list();
            OrderInfo.MyOrderItem myOrderItem = order_list.get(0);
            order_id = myOrderItem.getOrder_id();
            data = myOrderItem.getExtend_order_goods();
        }
        goodsListView = ViewFindUtils.find(view, R.id.goods_judge_list_view);
        goodsListView.setAdapter(new GoodsJudgeListAdapter(this, data));

        rb_01 = ViewFindUtils.find(view, R.id.evaluate_sudu10);
        rb_02 = ViewFindUtils.find(view, R.id.evaluate_taidu);
        rb_03 = ViewFindUtils.find(view, R.id.evaluate_sudu);
        cb_noName = ViewFindUtils.find(view, R.id.judge_no_name);
        Button btn_sure = ViewFindUtils.find(view,R.id.evaluate_submit);
        btn_sure.setOnClickListener(this);
    }

    private void judgeGoods() {
        StringRequest sr = new StringRequest(Request.Method.POST, Constant.URL_GOODS_JUDGE,
                new Response.Listener<String>() {
                    @Override
                    public void onResponse(String s) {
                        RLog.s("----judge--->" + s);
                        try {
                            JSONObject jsonObject = new JSONObject(s);
                            JSONObject datas = jsonObject.getJSONObject("datas");
                            String result = datas.optString("result");
                            String error = datas.optString("error");
                            if("1".equals(result)){
                                mHandler.obtainMessage(Constant.REQUEST_SUCCESS).sendToTarget();
                            }else{
                                mHandler.obtainMessage(Constant.REQUEST_FAIL,error).sendToTarget();
                            }
                        } catch (JSONException e) {

                            e.printStackTrace();
                        }
                    }
                }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError volleyError) {

            }
        }){
            @Override
            protected Map<String, String> getParams() throws AuthFailureError {
                Map<String,String> map = new HashMap<>();
                map.put("key",MyApplication.getmInstance().getKey());
                map.put("order_id",order_id);
                map.put("geval_isanonymous",cb_noName.isChecked()?"1":"0");
                map.put("store_desccredit",(int)rb_01.getRating() + "");
                map.put("store_servicecredit",(int)rb_02.getRating() + "");
                map.put("store_deliverycredit",(int)rb_03.getRating() + "");

//                Map<String,Object> ids = new HashMap<>();
                ArrayList<Map<String,Object>> list = new ArrayList<>();
                for (OrderInfo.MyOrderItem.ExtendOrderGoods eog : data) {
//                    Map<String,String> pairs = new HashMap<>();
                    map.put("goods[" + eog.getGoods_id() + "][geval_scores]",eog.getScore()==null?"5":eog.getScore());
                    map.put("goods[" + eog.getGoods_id() + "][geval_content]", eog.getContent()==null?"":eog.getContent());

//                    ids.put(eog.getGoods_id(), pairs);

                }
//                map.put("goods",ids.toString());

                RLog.s("--judge-->" + map.toString());
                return map;
            }
        };
        MyApplication.getmInstance().addToRequestQueue(sr);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.action_bar_left:
                finish();
                break;
            case R.id.evaluate_submit:
                goodsListView.clearFocus();
                judgeGoods();
                break;
        }
    }


}
